package com.erp.produce.service.impl;

import com.erp.common.util.ServiceUtil;
import com.erp.common.util.StringUtil;
import com.erp.produce.dao.ProductionTechnologyDao;
import com.erp.produce.entity.ProductionTechnology;
import com.erp.produce.service.ProductionTechnologyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductionTechnologyServiceImpl implements ProductionTechnologyService {

    @Autowired
    ProductionTechnologyDao dao;


    @Override
    public List<String> pTNameByGoodsId(Long goosId) {
        List<String> ptNames = new ArrayList<>();
        List<ProductionTechnology> list = dao.findByGoodsId(goosId);
        list.forEach((pt)->{ptNames.add(pt.getName());});
        return ptNames;
    }

    @Override
    public List<ProductionTechnology> findAll() {
        return dao.findAll(new Sort(Sort.Direction.ASC,"goodId"));
    }

    @Override
    public Boolean update(ProductionTechnology pt) {
        ProductionTechnology pt1 = dao.findById(pt.getId()).get();
        BeanUtils.copyProperties(pt,pt1);
        return dao.saveAndFlush(pt1) != null;
    }

    @Override
    public Boolean add(ProductionTechnology productionTechnology) {
        return dao.save(productionTechnology)!=null;
    }

    @Override
    public Boolean deleteIds(Long[] ids) {
        return ServiceUtil.deleteIds(ids,dao);
    }

    @Override
    public List<ProductionTechnology> listPage(int page, int size) {
        List<ProductionTechnology> content = dao.findAll(PageRequest.of(page, size)).getContent();

        return content;
    }

    @Override
    public List<ProductionTechnology> listPageLike(int page, int size, String column) {
        return dao.findAllByGoodsNameLike(PageRequest.of(page, size), StringUtil.addLike(column));
    }
}
